using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectCodeS.Models;

namespace ProjectCodeS.Controllers
{   
    public class ItemsController : Controller
    {
		private readonly IServiceTypeRepository servicetypeRepository;
		private readonly IItemRepository itemRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public ItemsController() : this(new ServiceTypeRepository(), new ItemRepository())
        {
        }

        public ItemsController(IServiceTypeRepository servicetypeRepository, IItemRepository itemRepository)
        {
			this.servicetypeRepository = servicetypeRepository;
			this.itemRepository = itemRepository;
        }

        //
        // GET: /Items/

        public ViewResult Index()
        {
            return View(itemRepository.AllIncluding(item => item.ServiceType, item => item.TicketItems));
        }

        //
        // GET: /Items/Details/5

        public ViewResult Details(int id)
        {
            return View(itemRepository.Find(id));
        }

        //
        // GET: /Items/Create

        public ActionResult Create()
        {
			ViewBag.PossibleServiceTypes = servicetypeRepository.All;
            return View();
        } 

        //
        // POST: /Items/Create

        [HttpPost]
        public ActionResult Create(Item item)
        {
            if (ModelState.IsValid) {
                itemRepository.InsertOrUpdate(item);
                itemRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleServiceTypes = servicetypeRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Items/Edit/5
 
        public ActionResult Edit(int id)
        {
			ViewBag.PossibleServiceTypes = servicetypeRepository.All;
             return View(itemRepository.Find(id));
        }

        //
        // POST: /Items/Edit/5

        [HttpPost]
        public ActionResult Edit(Item item)
        {
            if (ModelState.IsValid) {
                itemRepository.InsertOrUpdate(item);
                itemRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleServiceTypes = servicetypeRepository.All;
				return View();
			}
        }

        //
        // GET: /Items/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(itemRepository.Find(id));
        }

        //
        // POST: /Items/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            itemRepository.Delete(id);
            itemRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                servicetypeRepository.Dispose();
                itemRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

